
import conf from '../config/userconf.js'
export default{
    data(){
        return{
            list:[],  //列表数据
            authlist: [],  //权限列表
            searchData:{ //查询
                anm_branch_name:''
            },
            tableData: {
                id:'',
                name:'',
            },
            renewVisible: false,   //权限  弹框  隐藏显示
            authVisible: false,  //添加  编辑  权限弹框  隐藏显示
            authData: {   //权限表单
                name: '',
                type: '',
            },
            dialogtitle: '添加权限',  //弹框标题  权限
            module_id: '',  //模块id
            auth_id: '',  //权限id
            current: 1,  //模块页码
            authcurrent: 1,  //权限页码
            authrules: {
                name: [
                    { required: true, message: '请输入权限名称', trigger: 'blur' }
                ],
                type: [
                    { required: true, message: '请选择权限类型', trigger: 'change' }
                ],
            },
        }
    },
    methods:{
        /* 列表*/
        getList (data=Object()){
            data.page = this.current;
            conf.management.moduleList(data,(state,res)=>{
                if (state == 'success'&&res.data.code == 0){
                    // console.log(res);
                    this.list = res.data.data;
                }else if(state == 'success'&&res.data.code != 0){
                    this.list = [];
                    this.$message({
                        type: 'info',
                        message: res.data.msg
                    });
                }
            })
        },
        // 修改模块
        moduleEdit(row,data=Object()){
            this.$prompt('修改模块', {
                inputPlaceholder:'请输入模块名称',
                inputValue: row.name,
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(({ value }) => {
                data.name = value;
                data.id = row.id;
                conf.management.moduleEdit(data,(state,res)=>{
                    if(state == 'success'&&res.data.code == 0){
                        // console.log(res);
                        this.getList();
                        this.$message({
                            type: 'success',
                            message: '修改成功'
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消修改'
                });       
            });

        },
        // 添加模块
        moduleAdd(data=null){
            this.$prompt('添加模块', {
                inputPlaceholder:'请输入模块名称',
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(({ value }) => {
                data.name = value;  //模块id
                conf.management.moduleAdd(data,(state,res)=>{
                    if(state == 'success'&&res.data.code == 0){
                        this.getList();
                        this.$message({
                            type: 'success',
                            message: '添加成功'
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消添加'
                });       
            });
            

        },
        // 删除模块
        moduleDel(row,data=Object()){
             this.$confirm('确认删除模块 "'+row.name+'" ?', '删除模块', {  
                 confirmButtonText: '确定',
                 cancelButtonText: '取消',
                 type: 'warning'
             }).then(() => {
                 data.id = row.id;    //模块id
                 conf.management.moduleDel(data,(state,res)=>{
                     if(state == 'success'&&res.data.code == 0){
                        //  console.log(res);
                         this.getList();
                         this.$message({
                             type: 'success',
                             message: '删除成功!'
                         });
                     }else if(state == 'success'&&res.data.code != 0){
                         this.$message({
                             type: 'info',
                             message: res.data.msg
                         });
                     }
                 })
             }).catch(() => {
                 this.$message({
                   type: 'info',
                   message: '已取消删除'
                 });          
             });
         },
         //查看权限
        moduleLook(row,data=Object()){
            // console.log(row);
            this.module_id = row.id;

            //权限列表
            data.page = this.authcurrent;
            data.module_id = this.module_id;
            conf.management.authorityList(data,(state,res)=>{
                if (state == 'success'&&res.data.code == 0){
                    // console.log(res);
                    this.authlist = res.data.data;
                }else if(state == 'success'&&res.data.code != 0){
                    this.authlist = [];
                    this.$message({
                        type: 'info',
                        message: res.data.msg
                    });
                }
            })
            this.renewVisible = true;
        },
        /* 权限列表*/
        authList (data=Object()){
            data.page = this.authcurrent;
            data.module_id = this.module_id;
            conf.management.authorityList(data,(state,res)=>{
                if (state == 'success'&&res.data.code == 0){
                    // console.log(res);
                    this.authlist = res.data.data;
                }else if(state == 'success'&&res.data.code != 0){
                    this.authlist = [];
                    this.$message({
                        type: 'info',
                        message: res.data.msg
                    });
                }
            })
        },
        // 修改权限
        authorityEdit(row,data=Object()){
            this.auth_id = row.id;  //权限id
            this.authData.name = row.name;
            this.authData.type = row.type.toString();
            this.dialogtitle = '修改权限';
            this.authVisible = true;
        },
        
        // 添加权限
        authorityAdd(data=null){
            this.dialogtitle = '添加权限';
            this.authData.name = '';
            this.authData.type = '';
            this.authVisible = true;
        },
        /*取消 */
        cancel (formName) {
            this.authVisible = false;
            this.$refs[formName].resetFields();
        },
        /*提交 */
        submit (formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.judgeprimise();
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        /*判断提交 */
        judgeprimise (data=Object()) {
            if (this.dialogtitle == '添加权限') {
                data = this.authData; 
                data.module_id = this.module_id;  //模块id
                conf.management.authorityAdd(data,(state,res)=>{
                    if(state == 'success'&&res.data.code == 0){
                        this.authVisible = false;
                        this.authList();
                        this.$message({
                            type: 'success',
                            message: '添加成功'
                        });
                    }else if(state == 'success'&&res.data.code != 0){
                        this.$message({
                            type: 'info',
                            message: res.data.msg
                        });
                    }
                })
            }else if (this.dialogtitle == '修改权限'){
                data = this.authData; 
                data.module_id = this.module_id;  //模块id
                data.id = this.auth_id;  //权限id
                conf.management.authorityEdit(data,(state,res)=>{
                    if (state == 'success'&&res.data.code == 0){
                        this.authVisible = false;
                        this.authList();
                        this.$message({
                            type: 'success',
                            message: '修改成功!'
                        });
                    }else if(state == 'success'&&res.data.code != 0){
                        this.$message({
                            type: 'info',
                            message: res.data.msg
                        });
                    }
                })
            }
        },
        // 删除权限
        authorityDel(row,data=Object()){
             this.$confirm('确认删除权限 "'+row.name+'" ?', '删除权限', {  
                 confirmButtonText: '确定',
                 cancelButtonText: '取消',
                 type: 'warning'
             }).then(() => {
                 data.id = row.id;    //权限id
                 conf.management.authorityDel(data,(state,res)=>{
                     if(state == 'success'&&res.data.code == 0){
                        //  console.log(res);
                         this.authList();
                         this.$message({
                             type: 'success',
                             message: '删除成功!'
                         });
                     }else if(state == 'success'&&res.data.code != 0){
                         this.$message({
                             type: 'info',
                             message: res.data.msg
                         });
                     }
                 })
             }).catch(() => {
                 this.$message({
                   type: 'info',
                   message: '已取消删除'
                 });          
             });
        },
        // 模块  切换页码
        handleCurrentChange(val) {
            this.current = val;
            this.getList();
        },
        // 权限  切换页码
        authhandleCurrentChange(val) {
            this.authcurrent = val;
            this.authList();
        },
        // 权限类型
        authType (row, column, cellValue, index) {
            switch (cellValue) {
                case 1:
                    return '按钮权限'
                case 2:
                    return '字段权限'
            }
        },
    },
    mounted (){
        this.$nextTick(function(){
            this.getList()
        })
    },
}